package com.example.myblog.service.impl;

import com.example.myblog.model.Settings;
import com.example.myblog.repository.SettingsRepository;
import com.example.myblog.service.SettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 网站设置服务实现类
 */
@Service
public class SettingsServiceImpl implements SettingsService {

    @Autowired
    private SettingsRepository settingsRepository;

    @Override
    public List<Settings> getAllSettings() {
        return settingsRepository.findAll();
    }

    @Override
    public Settings getSettingByKey(String key) {
        return settingsRepository.findBySettingKey(key).orElse(null);
    }

    @Override
    public String getSettingValueByKey(String key, String defaultValue) {
        Settings setting = getSettingByKey(key);
        return setting != null ? setting.getSettingValue() : defaultValue;
    }

    @Override
    public Settings saveSetting(String key, String value, String description) {
        Settings setting = getSettingByKey(key);
        if (setting == null) {
            setting = new Settings();
            setting.setSettingKey(key);
        }
        setting.setSettingValue(value);
        setting.setDescription(description);
        return settingsRepository.save(setting);
    }

    @Override
    public void deleteSetting(String key) {
        Settings setting = getSettingByKey(key);
        if (setting != null) {
            settingsRepository.delete(setting);
        }
    }
}